package stardust.core.support;


import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class PatternUtils
{

  private static String[] split(String content)
  {
    List<String> result = new ArrayList<String>();
    char[] contentArray = content.toCharArray();
    String temp = "";

    for (int i = 0; i < contentArray.length; i++)
    {
      if (contentArray[i] == '%')
      {
        if (temp.equals("%"))
        {
          continue;
        }

        if (temp.length() > 0)
        {
          result.add(temp);
        }

        temp = "%";
        result.add(temp);
      }
      else
      {
        if (temp.equals("%"))
        {
          temp = "";
        }

        temp += contentArray[i];

        if (i == contentArray.length - 1)
        {
          result.add(temp);
        }
      }
    }

    return result.toArray(new String[0]);
  }

  public static String getSimpleFetchPattern(String pattern)
  {
    return getFetchPattern(pattern)[0][1];
  }

  private static String[][] getFetchPattern(String pattern)
  {
    List<String[]> result = new ArrayList<String[]>();
    String buffer = "";
    String buffer2 = "";
    String[] dat = split(pattern);

    for (int i = 0; i < dat.length; i++)
    {
      if (dat[i].equals("%"))
      {
        if (i == 0)
        {
          buffer = "^";
        }
        else if (i > 0)
        {
          buffer += "(?<=" + (dat[i - 1]) + ")";
          buffer2 += (dat[i - 1]);
        }

        buffer += (".*?");
        buffer2 += ("%");

        if (i == dat.length - 1)
        {
          buffer += "$";
        }
        else if (i < dat.length - 1)
        {
          buffer += "(?=" + (dat[i + 1]) + ")";
          buffer2 += (dat[i + 1]);
        }

        result.add(new String[]
        {
          buffer2, buffer
        });
        buffer = buffer2 = "";
      }
    }

    return result.toArray(new String[0][0]);
  }

  public static List<String> fetchParameter(String reg, String content)
  {
    // StringBuffer buffer = new StringBuffer();
    List<String> result = new ArrayList<String>();
    String[][] pattern = getFetchPattern(reg);

    while (content.replaceAll("%%", "%").indexOf("%%") != -1)
      ;

    for (int i = 0; i < pattern.length; i++)
    {
      Pattern p = Pattern.compile(pattern[i][1], Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
      Matcher m = p.matcher(content);

      if (m.find())
      {
        for (int j = 0; j <= m.groupCount(); j++)
        {
          // 特殊处理，只返回第一个获取的参数，而且不带NAME=VALUE;这种格式，直接就是VALUE
          // return m.group(j);
          result.add(m.group(j));
          // buffer.append(pattern[i][0]).append("=").append(m.group(j)).append(";");
        }
      }
    }

    return result;
  }

  public static void main(String[] args)
  {
    /*
     * String content3 = "FSFDSFSD\n代扣款（扣款用途：代扣佣金 [925805030\n51903]）"; String
     * content2 = "代扣款（扣款用途：代扣返点积分91984807128433）"; String content1 =
     * "信用卡支付服务费[2011090865774800]";
     * System.out.println(PatternUtils.fetchParameter("代扣款（扣款用途：代扣佣金 \\[%\\]）",
     * content3));
     * System.out.println(PatternUtils.fetchParameter("代扣款（扣款用途：代扣返点积分%）",
     * content2));
     * System.out.println(PatternUtils.fetchParameter("信用卡支付服务费\\[%\\]",
     * content1));
     */

    System.out.println(PatternUtils.fetchParameter("哈哈%嘿嘿嘿", "哈哈哈哈哈呵呵呵嘿嘿嘿"));
  }

}
